package com.yun.yun;

import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Set;

public final class ShellExecute {
	private static final String	TAG	= "ShellExecute";

	//判断Android设备是否拥有Root权限
	public static boolean isDeviceRooted(){
		 String[] locations = {"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/",
	                "/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/",
	                "/system/sbin/", "/usr/bin/", "/vendor/bin/"};
	        for (String location : locations) {
	            if (new File(location + "su").exists()) {
	                return true;
	            }
	        }
	        return false;
	}
	
	// --执行adb 命令--//
	private static void execShell(String cmd) {
		try {
			//在单独的进程中执行指定的字符串命令
			Process process = Runtime.getRuntime().exec("su");
			// 输出流
			DataOutputStream dataOutputStream = new DataOutputStream(process.getOutputStream());
			// 输入流
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			dataOutputStream.writeBytes(cmd);
			dataOutputStream.flush();
			dataOutputStream.close();

			// 读取执行信息
			int length = 0;
			char[] buffer = new char[4096];
			StringBuffer output = new StringBuffer();
			while ((length = reader.read(buffer)) > 0) {
				output.append(buffer, 0, length);
			}
			if(reader != null)
				reader.close();
			if(output != null && output.length() > 0)
				Log.d(TAG, "execShell retsultMsg:" + output.toString());
			
			process.waitFor();
            //process.exitValue();
			
			// outputStream.close();
		} catch (Throwable t) {
			t.printStackTrace();
			Log.d(TAG, "execShell " + cmd + " " + t.getMessage());
		}
	}

	// Executes UNIX command.
	//adb shell getenforce
	/*
	 * Enforcing：强制模式。代表SELinux在运行中，且已经开始限制domain/type之间的验证关系
	 * Permissive：宽容模式。代表SELinux在运行中，不过不会限制domain/type之间的验证关系，即使验证不正确，进程仍可以对文件进行操作。不过如果验证不正确会发出警告
	 */
	//sestatus 命令功能：查看当前系统上面SELinux的策略，运行模式等信息
	public static String getSELinuxMode() {
		String command = "getenforce";//查看当前SELinux的运行模式
		try {
			Process process = Runtime.getRuntime().exec(command);
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			int length = 0;
			char[] buffer = new char[4096];
			StringBuffer output = new StringBuffer();
			while ((length = reader.read(buffer)) > 0) {
				output.append(buffer, 0, length);
			}
			reader.close();
			process.waitFor();
			return output.toString().trim();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	public static void chmodShell(ArrayList<String> strCmdList) {
		for (int i = 0; i < strCmdList.size(); i++) {
			execShell(strCmdList.get(i));
		}
	}

	public static void chmodShell(Set<String> strCmdList) {
		for (String strcmd : strCmdList) {
			execShell(strcmd);
		}
	}
	
	private static boolean isExecutable(String filePath) {
        Process p = null;
        try {
            p = Runtime.getRuntime().exec("ls -l " + filePath);
            // 获取返回内容
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String str = in.readLine();
            Log.i(TAG, str);
            if (str != null && str.length() >= 4) {
                char flag = str.charAt(3);
                if (flag == 's' || flag == 'x')
                    return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (p != null) {
                p.destroy();
            }
        }
        return false;
    }
}

